'''
Author: your name
Date: 2020-11-07 22:17:30
LastEditTime: 2020-11-09 16:17:51
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \dblp_paper_search\zotero_dblp.py
'''
import requests
import os
import re
session=requests.Session()
headers = {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
           "Cache-Control": "max-age=0", "Upgrade-Insecure-Requests": "1",
           "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0",
           "Referer": "https://dblp.org/search?q=NDSS", "Connection": "close",
           "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
           "Accept-Encoding": "gzip, deflate"}
cookies = {"dblp-search-mode": "c", "dblp-dismiss-new-feature-2019-08-19": "3"}
def get_all_bibtex(url):
    bibtexs=[]
    res = session.get(url, headers=headers, cookies=cookies)
    data = res.content.decode('utf-8')
    # print(data)
    assert res.status_code==200
    bibtex_urls = re.compile(
        r'https://dblp\.org/rec/[a-zA-Z0-9/.-]{1,70}\?view=bibtex').findall(data)
    bibtex_urls=list(set(bibtex_urls))
    for l in bibtex_urls:
        bibtex=get_one_bibtex(l)
        bibtexs.append(bibtex)
    bibtexs=list(set(bibtexs))
    return bibtexs
def get_one_bibtex(url):
    res=session.get(url,headers=headers,cookies=cookies)
    data = res.content.decode('utf-8')
    assert res.status_code==200
    try:
        bibtex = re.compile(
            r'@[in]*proceedings{.*}', re.S).findall(data)[0]
        print(bibtex)
        return bibtex
    except Exception as e:
        try:
            bibtex = re.compile(
                r'@article{.*}', re.S).findall(data)[0]
            print(bibtex)
            return bibtex
        except Exception as e:
            try:
                bibtex = re.compile(
                    r'@phdthesis{.*}', re.S).findall(data)[0]
                print(bibtex)
                return bibtex
            except Exception as e:
                print(e)
                print(data)
                return '{}:error!'.format(url)
def main():
    path="./bibtex"
    if not os.path.exists(path):
        os.mkdir(path)
    type=input('input what type of search(person, conf, journals):')
    if type=='person':
        name=input('input name of the person:')
        output="{name}.bibtex".format(path=path,name=name)
        url=input('input url of the person in dblp:')
        results=get_all_bibtex(url)
        tmp='\n'.join(results)
        with open('{path}/{name}'.format(path=path,name=output),'w') as f:
            f.write(tmp)
    elif type=='conf' or type=='journals':
        name = input('input name of the {}:'.format(type))
        start_year=int(input('input start year:'))
        end_year=int(input('input end year:'))
        for year in range(start_year,end_year+1):
            output = "{name}_{year}.bibtex".format(path=path, name=name, year=year)
            url = "https://dblp.org/db/{type}/{name}/{name}{year}.html".format(type=type,name=name,year=year)
            results = get_all_bibtex(url)
            tmp = '\n'.join(results)
            with open('{path}/{name}'.format(path=path, name=output), 'w') as f:
                f.write(tmp)
    else:
        print('Input Type of Search error!')
if __name__=='__main__':
    main()
